package com.cgbl.cg.ui.course

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.LimitedTimeAdapter
import com.cgbl.cg.adapter.MoreCourseAdapter
import com.cgbl.cg.databinding.AppActivityExcellentCourseBinding
import com.cgbl.cg.view.FilterPop
import com.cgbl.cg.vm.course.ExcellentCourseViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseBean
import com.robot.base.model.PreferentPageBean

@Route(path = AppConstants.Router.COURSE.A_EXCELLENT_COURSE)
class ExcellentCourseActivity :
    GLBaseActivity<AppActivityExcellentCourseBinding, ExcellentCourseViewModel>() {
    private lateinit var moreCourseAdapter: MoreCourseAdapter
    private lateinit var limitedTimeAdapter: LimitedTimeAdapter
    var type = 0

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_excellent_course

    override fun initVariableId(): Int = BR.viewModel

    override fun initParam() {
        super.initParam()
        type = intent.getIntExtra(AppConstants.BundleKey.TYPE, 0)
    }

    override fun initComponents() {
        addSingleTitleBar(
            when (type) {
                0 -> {
                    getString(R.string.app_en_tips_53)
                }
                1 -> {
                    getString(R.string.app_en_tips_42)
                }
                2 -> {
                    getString(R.string.app_en_tips_54)
                }
                3 -> {
                    getString(R.string.app_en_tips_46)
                }
                else -> {
                    getString(R.string.app_en_tips_53)
                }
            }
        )
        viewModel.type.set(type)
        initAdapter()
        viewModel.initData()
    }

    override fun initData() {
        super.initData()
    }

    private fun initAdapter() {
        moreCourseAdapter = MoreCourseAdapter()
        limitedTimeAdapter = LimitedTimeAdapter()
        binding.recyclerView.apply {
            adapter = if (type != 3) {
                moreCourseAdapter
            } else {
                limitedTimeAdapter
            }
        }

    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.showFilter.observe(this) {
            val filterPop = FilterPop(this, viewModel.filterName.get() ?: "", viewModel.list)
            XPopup.Builder(this)
                .atView(binding.tv1)
                .asCustom(filterPop)
                .show()

            filterPop.setConfirmClickListener {
                filterPop.dismiss()
                viewModel.filterName.set(it)
                viewModel.filterType = viewModel.list.indexOf(it)
                viewModel.refresh()
            }
        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        binding.smartRefreshLayout.setOnLoadMoreListener {
            viewModel.load()
        }

        viewModel.uc.refreshEvent.observe(this) {
            binding.smartRefreshLayout.finishRefresh()
            binding.smartRefreshLayout.finishLoadMore()
            binding.smartRefreshLayout.setEnableLoadMore(it != 1)
        }

        viewModel.uc.refreshDataEvent.observe(this) {
            if (viewModel.pageNo == 1) {
                moreCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
            } else {
                moreCourseAdapter.addData(it)
            }
        }

        viewModel.uc.refreshLimitedTimeDataEvent.observe(this) {
            if (viewModel.pageNo == 1) {
                limitedTimeAdapter.setNewInstance(it as MutableList<PreferentPageBean>?)
            } else {
                limitedTimeAdapter.addData(it)
            }
        }
    }
}